// ManualBot.cpp : Defines the entry point for the console application.
//

#define _CRT_SECURE_NO_DEPRECATE 1
#include "stdafx.h"
//#include "gnuplot_i.hpp"
#include "Grapher.h"
#include "Graph.h"
#include "UniformSearch.h"
#include "AStarSearch.h"
#include "DepthFirstSearch.h"
#include "BreadthFirstSearch.h"
#include "IterativeDeepeningSearch.h"

#include <conio.h>   //for getch(), needed in wait_for_key()
#include <windows.h> //for Sleep()
void sleep(int i) { Sleep(i*1000); }
long GetTime();


int _tmain(int argc, char* argv[])
{
	string server;
	int port;

	if(argc < 2) {
		cout << "server: ";
		cin >> server;
	}
	else {
		server = argv[1];
	}
    if(argc < 3) {
		cout << "port: ";
		cin >> port;
	}
	else {
        port = atoi(argv[2]);
    }

	Remote::init(server, port, false);
	if (!Remote::instance()->GetStatus()) {
		cout << "Unable to contact server" << endl;
		exit(1);
	}

	BZRC* remote = Remote::instance();
	World* world = World::instance();

	/*while (true) {
		long t1 = GetTime();

		// First update our understanding of the world
		world->update();
		// Then have our team react to that knowledge

		long t2 = GetTime();
		cout << t2 - t1 << endl;
	}*/

	world->update();

	Graph g;
	Grapher gr("test.gpi");

	Vector2 start;
	Vector2 goal;

	UniformSearch us;
	AStarSearch as;
	DepthFirstSearch dfs;
	BreadthFirstSearch bfs;
	IterativeDeepeningSearch idfs;

	start.x = world->tanks()[0].pos[0];
	start.y = world->tanks()[0].pos[1];

	goal.x = world->flags()[2].pos[0];
	goal.y = world->flags()[2].pos[1];

	remote->get_occgrid(0, &g);

	for (int i = 0; i < world->other_tanks().size(); i++) {
		g.setObstacle(Vector2(
			world->other_tanks().at(i).pos[0],
			world->other_tanks().at(i).pos[1]));
	}

	g.buildEdges();

	//us.displaySearch(g, start, goal, gr, true, 10, false, "uniform.gpi", Vector2(-200, 0), Vector2(-100, 100), 3000);
	//us.displaySearch(g, start, goal, gr, true, 500, true, "uniform2.gpi");
	//as.displaySearch(g, start, goal, gr, true, 10, false, "astar.gpi", Vector2(-200, 0), Vector2(-100, 100), 3000);
	//as.displaySearch(g, start, goal, gr, true, 100, true, "astar2.gpi");
	dfs.displaySearch(g, start, goal, gr, true, 10, false, "depthfirst.gpi", Vector2(-200, 0), Vector2(-100, 100), 3000);
	//dfs.displaySearch(g, start, goal, gr, true, 500, true, "depthfirst2.gpi");
	//bfs.displaySearch(g, start, goal, gr, true, 10, false, "breathfirst.gpi", Vector2(-200, 0), Vector2(-100, 100), 3000);
	//bfs.displaySearch(g, start, goal, gr, true, 500, true, "breathfirst2.gpi");
	//idfs.displaySearch(g, start, goal, gr, true, 10, false, "iterativedeepening.gpi", Vector2(-200, 0), Vector2(-100, 100), 5983);
	//idfs.displaySearch(g, start, goal, gr, true, 500, true, "iterativedeepening2.gpi");
	
	return 0;
}

long GetTime() {
	SYSTEMTIME tm;
	GetSystemTime(&tm);
	return (long)tm.wMilliseconds + 
		   (long)tm.wSecond * 1000 + 
		   (long)tm.wMinute * 60000 + 
		   (long)tm.wHour * 3600000;// + 
		   //(long)tm.wDay * 86400000;
}